package com.yanqu.road.server.cmd.beauty;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.pb.beauty.BeautyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_BEAUTY_GIVE_GIFT, desc = "美女赏赐")
public class GiveBeautyGiftCmd extends AbstractCommand {

    private int beautyId;

    private int giftId;

    private int giftCount;

    private int addIntimacy;

    private int addGlamour;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyProto.BeautyGiftRewardReqMsg req = BeautyProto.BeautyGiftRewardReqMsg.parseFrom(packet.getBytes());
        beautyId = req.getBeautyId();
        giftId = req.getGiftId();
        giftCount = req.getGiftCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (giftCount < 1) {
            ret = GameErrorCode.E_BAG_USE_GOODS_COUNT_LESS_ONE;
            return;
        }

        if (giftCount == 1) {

        }else if (giftCount <= 10) {
            String[] split = GameConfig.GIVE_BEAUTY_GIFT_TEN_VIP_LIMIT.split(";");
            if (VipMgr.getVipLv(player.getUserInfo().getVipExp()) < Integer.parseInt(split[0])
            && player.getTitleId() < Integer.parseInt(split[1])) {
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
            //美女数量限制
            int beautyNum = player.getModule(BeautyModule.class).getBeautyMap().size();
            if(beautyNum < GameConfig.GIVE_BEAUTY_GIFT_TEN_BEAUTY_NUM_LIMIT){
                ret = GameErrorCode.E_BEAUTY_TEN_GIVE_LIMIT_BY_NUM;
                return;
            }
        }else if(giftCount <= 100){
            if(VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.GIVE_BEAUTY_GIFT_HUNDRED_VIP_LIMIT){
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
        }else {
            ret = GameErrorCode.E_BEAUTY_GIVE_GIFT_COUNT_OVER_LIMIT;
            return;
        }

        UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(beautyId);
        if (!BeautyMgr.isUserBeautyExist(userBeauty)) {
            ret = GameErrorCode.E_BEAUTY_NO_FOUND;
            return;
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(giftId);

        if (giftCount > 1) {
            Property beautyGift = new Property(giftId, BigInteger.valueOf(giftCount));
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(beautyGift)) {
                player.notifyListener(eGamePlayerEventType.RechargeBeautyCount.getValue(), 0);
                player.notifyListener(eGamePlayerEventType.RechargeBeautyCountNew.getValue(), 0);
                long userBagItemCount = player.getModule(BagModule.class).getUserBagItemCount(giftId);
                giftCount = (int) userBagItemCount;
            }
        }

        if (GoodsMgr.isBeautyIntimacy(goodsInfo)) {
            intimacyGift(player);
        } else if (GoodsMgr.isBeautyGlamour(goodsInfo)) {
            glamourGift(player);
        } else {
            ret = GameErrorCode.E_BAG_GOODS_NO_BEAUTY_GIFT;
        }
    }

    private void intimacyGift(GamePlayer player) {
        //检测道具合法性
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(giftId);
        if (!GoodsMgr.isBeautyIntimacy(goodsInfo)) {
            ret = GameErrorCode.E_BAG_GOODS_NO_BEAUTY_INTIMACY;
            return;
        }
        UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(beautyId);
        int oldIntimacy = userBeauty.getIntimacy();
        if (oldIntimacy >= GameConfig.BEAUTY_MAX_INTIMACY_BY_REWARD) {
            ret = GameErrorCode.E_BEAUTY_REWARD_ADD_INTIMACY_IS_MAX;
            return;
        }

        //计算能增加的亲密度
        int intimacy = goodsInfo.getParamList().get(0).intValue();
        int realUseCount = giftCount;
        int offset = GameConfig.BEAUTY_MAX_INTIMACY_BY_REWARD - oldIntimacy;
        if (intimacy * giftCount > offset) {
            realUseCount = (offset + intimacy - 1) / intimacy;
        }
        if (realUseCount > giftCount) {
            realUseCount = giftCount;
        }

        addIntimacy = intimacy * realUseCount;
        if (addIntimacy > offset) {
            addIntimacy = offset;
        }
        //增加亲密度和赏赐次数
        ret = player.getModule(BeautyModule.class).giveGift(userBeauty, addIntimacy, giftId, realUseCount);
    }

    private void glamourGift(GamePlayer player) {
        //检测道具合法性
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(giftId);
        if (!GoodsMgr.isBeautyGlamour(goodsInfo)) {
            ret = GameErrorCode.E_BAG_GOODS_NO_BEAUTY_GLAMOUR;
            return;
        }

        UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(beautyId);
        int glamour = goodsInfo.getParamList().get(0).intValue();

        addGlamour = glamour * giftCount;

        //增加亲密度或魅力值和赏赐次数
        ret = player.getModule(BeautyModule.class).giveGift(userBeauty, addGlamour, giftId, giftCount);
    }

    @Override
    public void sendData(GamePlayer player) {
        BeautyProto.BeautyGiftRewardRespMsg.Builder respMsg = BeautyProto.BeautyGiftRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (0 == ret) {
            respMsg.setAddIntimacy(addIntimacy);
            respMsg.setAddGlamour(addGlamour);
        }
        player.sendPacket(Protocol.U_BEAUTY_GIVE_GIFT, respMsg);
    }
}
